package sudoku_gui_handler;

import sudoku_gui.*;
import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.SpinnerListModel;
import javax.swing.SwingConstants;
import javax.swing.plaf.basic.BasicArrowButton;
import javax.swing.plaf.basic.BasicSpinnerUI;
import sudoku_properties.SudokuGameInfoParam;

/*********************************************************************
 * Class SpinnerHandler
 * 1. handle the spinner event
 *********************************************************************/
public class SpinnerHandler{

    public SpinnerHandler(){}

    public SpinnerHandler(JSpinner inputSpinner, JPanel inputStartGamePanel){
        spinner = inputSpinner;
        startGamePanel = inputStartGamePanel;
        defaultIndex = 0;
    }

    public void setupSpinner(int inputStartPosX, int inputStartPosY, String[] inputSpinnerValues, int inputDefaultIndex){
        /* Define local variables*/
        startPosX = inputStartPosX;
        startPosY = inputStartPosY;
        spinnerValues = inputSpinnerValues;
        defaultIndex = inputDefaultIndex;

        /* setup spinner properties */
        spinner = new JSpinner(new SpinnerListModel(spinnerValues));
        spinner.setBorder(BorderFactory.createEmptyBorder());
        /* default the diaplay value of index length-1 */
        spinner.setValue( spinnerValues[defaultIndex]);
        spinner.setBounds(startPosX, startPosY, WSIZE_SPINNER, LSIZE);
        spinner.setUI(new spinnerUI());

        /* use editor to setup the background color of text field*/
        JSpinner.DefaultEditor editor  = (JSpinner.DefaultEditor)spinner.getEditor();
        editor.getTextField().setBackground(SudokuComponentsProperties.getColorBackground());
        editor.getTextField().setHorizontalAlignment(SudokuComponentsProperties.getAlgimentLeft());
        spinner.setEditor(editor);
        startGamePanel.add(spinner);
    }

    /* Code copied form http://forums.sun.com/thread.jspa?threadID=623704 start ***********************************************/
    class spinnerUI extends BasicSpinnerUI{

        @Override
        protected Component createNextButton(){
            JButton btn = (JButton)super.createNextButton();
            JButton btnNext = new MyBasicArrowButton(SwingConstants.NORTH);
            btnNext.addActionListener(btn.getActionListeners()[0]);
            btnNext.setBackground(SudokuComponentsProperties.getColorBackground());
            return btnNext;
        }
        @Override
        protected Component createPreviousButton()
        {
            JButton btn = (JButton)super.createPreviousButton();
            JButton btnPrevious = new MyBasicArrowButton(SwingConstants.SOUTH);
            btnPrevious.addActionListener(btn.getActionListeners()[0]);
            btnPrevious.setBackground(SudokuComponentsProperties.getColorBackground());
            return btnPrevious;
        }

    }

    class MyBasicArrowButton extends BasicArrowButton{

        public MyBasicArrowButton(int direction){
            super(direction);
        }
        public MyBasicArrowButton(int direction,Color background,Color shadow,Color darkShadow,Color highlight){
            super(direction,background,shadow,darkShadow,highlight);
        }
        @Override
        // only for color setting
        public void paintTriangle(Graphics g,int x,int y,int size,int direction,boolean isEnabled){

            Color oldColor = g.getColor();//Note 1: all if(!isEnabled) code removed, for brevity
            int mid, i, j;                //Note 2: all EAST / WEST code removed, for brevity
            j = 0;
            size = Math.max(size, 2);
            mid = (size / 2) - 1;
            g.translate(x, y);

            g.setColor(SudokuComponentsProperties.getColorSpinnerArrow());
            if(!isEnabled){
                g.setColor(SudokuComponentsProperties.getColorSpinnerArrowDisabled());
            }
            switch(direction){
                case NORTH:
                    for(i = 0; i < size; i++){
                        g.drawLine(mid-i, i, mid+i, i);
                    }
                    if(!isEnabled)  {
                        g.setColor(SudokuComponentsProperties.getColorSpinnerArrowDisabled());
                        g.drawLine(mid-i+2, i, mid+i, i);
                    }
                    break;
                case SOUTH:
                    if(!isEnabled)  {
                        g.translate(1, 1);
                        g.setColor(SudokuComponentsProperties.getColorSpinnerArrowDisabled());
                        for(i = size-1; i >= 0; i--)   {
                            g.drawLine(mid-i, j, mid+i, j);
                            j++;
                        }
                        g.translate(-1, -1);
                        g.setColor(SudokuComponentsProperties.getColorSpinnerArrowDisabled());
                    }
                    j = 0;
                    for(i = size-1; i >= 0; i--){
                        g.drawLine(mid-i, j, mid+i, j);
                        j++;
                    }
                    break;
            }
            g.translate(-x, -y);
            g.setColor(oldColor);
        }
    }
    /* Code copied form http://forums.sun.com/thread.jspa?threadID=623704 end ******************************************************/

    public void setSpinnerEnabled(boolean isEnabled){
        spinner.setEnabled(isEnabled);
    }

    public String getSpinnerValue(){
        String spinnerValue = (String) spinner.getValue();
        return spinnerValue;
    }
    public String getSpinnerValueFromChin(){
        String spinnerValue = (String) spinner.getValue();
        return spinnerValue;
    }

    /* Input Spinner and Panel */
    private JSpinner spinner;
    private JPanel startGamePanel;

    /* Spinner properties */
    private int startPosX;
    private int startPosY;
    private String[] spinnerValues;
    private String[] spinnerDisplay;
    private int defaultIndex;
    
    /* Constants */
    final int LSIZE = SudokuComponentsProperties.getLSize();
    final int WSIZE_SPINNER = SudokuComponentsProperties.getWSizeSpinner();



}
